<template>
<div class="entering-merchant views-container">
    <div class="wlm-table">
        <div class="wlm-table-hearder-btn">
            <router-link tag="span" to="/channel/weChatReplyChannelListAdd">
                <el-button size="small" type="primary">添加回复</el-button>
            </router-link>
        </div>
        <div v-if="tableFormatData.userTable.files.status == '1'" class="wlm-table-notice warning">
            <div class="flex-row flex-justify-b flex-align-c">
                <p>用户关注公众号时，发送的欢迎信息。</p>
            </div>
        </div>
        <div class="wlm-table-header">
            <el-form :model="tableFormatData.userTable.files" size="small" label-width="80px" class="retail-form" label-position="right">
                <el-form-item label="" class="filter">
                    <el-form-item label="关键字：" class="search">
                        <el-input @keyup.enter.native="filesSerch" v-model="tableFormatData.userTable.files.keyword" placeholder="请输入关键字" style="width:175px;"></el-input>
                    </el-form-item>
                    <el-form-item label="回复类型：" class="search">
                        <el-select v-model="tableFormatData.userTable.files.keyword_type" placeholder="请选择">
                            <el-option v-for="item in [{lable:'全部',value:''},{lable:'回复图文',value:'news'},{lable:'回复音频',value:'music'},{lable:'回复文字',value:'basic'},{lable:'回复图片',value:'images'},{lable:'回复视频',value:'video'}]" :key="item.value" :label="item.lable" :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-form-item>
                <el-form-item label="" class="filter-btns">
                    <el-button type="primary" @click="filesSerch">筛选</el-button>
                    <el-button type="text" @click="filesEmpty">清空筛选条件</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div class="wlm-table-content">
            <el-tabs v-model="tableFormatData.userTable.files.module" type="card" @tab-click="filesSerch">
                <el-tab-pane label="关键字自动回复" name="reply"></el-tab-pane>
                <el-tab-pane label="关注回复" name="first"></el-tab-pane>
                <el-tab-pane label="默认回复" name="default"></el-tab-pane>
                <el-tab-pane label="搜索回复" name="search"></el-tab-pane>
            </el-tabs>
            <el-table :ref="tableFormatData.userTable.key" :data="tableFormatData.userTable.tableData" @selection-change="handleSelectionChange" style="width: 100%">
                <el-table-column type="selection" width="55">
                </el-table-column>
                <el-table-column prop="date" label="规则名称" >
                    <template slot-scope="scope">
                        <p>{{scope.row.name}}</p>
                    </template>
                </el-table-column>
                <el-table-column prop="name" label="关键字" >
                    <template slot-scope="scope">
                        <span class="text-danger">{{$keyword(scope.row)['keys']}}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="name" label="回复内容" >
                    <template slot-scope="scope">
                        <el-popover trigger="hover" placement="top">
                            <div class="flex-col">
                                <div class="flex-row flex-align-c flex-justify-c">
                                    <span v-html="$keyword(scope.row)['content']"></span>
                                </div>
                            </div>
                            <div slot="reference" class="name-wrapper">
                                <div class="ellips_line2" style="max-height: 51px;overflow: hidden;text-align: left;">
                                    <span v-html="$keyword(scope.row)['content']"></span>
                                </div>
                            </div>
                        </el-popover>
                    </template>
                </el-table-column>
                <el-table-column prop="name" label="开关" >
                    <template slot-scope="scope">
                        <el-switch :active-value="0" :inactive-value="1" @change="changeTableItem({ id:scope.row.id ,status:scope.row.status,type: tableFormatData.userTable.files.module},tableFormatData.userTable.change.status,{isWarning: scope.row.status === 1,warning:'确定关闭吗？',cancel:{msg:'已取消关闭',val:{scope:scope,cancelStatus:{key:'status',val:0}}}})" v-model="scope.row.status"> </el-switch>
                    </template>
                </el-table-column>
                <el-table-column label="操作" >
                    <template slot-scope="scope">
                        <div class="operation-group">
                            <el-button class="wlm-text" type="text" @click="$router.push({path:'/channel/weChatReplyChannelListAdd', query: { id: scope.row.id }})">编辑</el-button>
                            <div class="btn-line"></div>
                            <el-button class="wlm-text" type="text" @click="delTableItem(scope.row.id)">删除</el-button>
                        </div>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-content flex-row flex-justify-b flex-align-c">
                <el-row class="pagination-btns">
                    <el-checkbox class="check-all" true-label="1" false-label="0" v-model="tableFormatData.userTable.files.checkall" @change="toggleSelection">全部</el-checkbox>
                    <el-button class="right-8" :disabled="isGroup" size="mini" @click="delTableList">删除</el-button>
                </el-row>
                <el-pagination :disabled="!hasTableData" @size-change="listPageChange" @current-change="listPageChange" :current-page.sync="tableFormatData.userTable.pagination.page" :page-sizes="tableFormatData.userTable.pagination.pagesizes" :page-size.sync="tableFormatData.userTable.pagination.list_rows" layout="total, sizes, prev, pager, next, jumper" :total="hasTableData?tableFormatData.userTable.pagination.total : 0">
                </el-pagination>
            </div>
        </div>
    </div>
</div>
</template>

<script>
import {
  getReplys,
  deleteReply,
  statusReply
} from '@/api/channel'
import mixins from '@/mixins/mixins'
export default {
  mixins: [mixins.getters('Table')],
  name: 'WeChatReplyChannelList',
  components: {
  },
  created() {
  },
  computed: {
    $keyword() {
      return function($scope) {
        const result = {
          keys: [],
          content: []
        }
        const { keywords, allreply } = $scope

        this.keywordMap.map(item => {
          if (allreply && allreply[`${item}`]) {
            const { basic, images, music, news, video, newsText, sappCard } = allreply
            console.log(allreply, '===============')
            switch (item) {
              case 'basic':
                result.content.push(`<span class="text-danger">${basic}</span>条文本`)
                break
              case 'images':
                result.content.push(`<span class="text-danger">${images}</span>条图片`)
                break
              case 'music':
                result.content.push(`<span class="text-danger">${music}</span>条音频`)
                break
              case 'news':
                result.content.push(`<span class="text-danger">${news}</span>条图文`)
                break
              case 'newsText':
                result.content.push(`<span class="text-danger">${newsText}</span>条图文链接`)
                break
              case 'sappCard':
                result.content.push(`<span class="text-danger">${sappCard}</span>条小程序卡片`)
                break
              case 'video':
                result.content.push(`<span class="text-danger">${video}</span>条视频`)
                break
            }
          }
        })
        if (keywords.length > 0) {
          keywords.map(item => {
            const { content } = item
            if (content) {
              result.keys.push(content)
            }
          })
        }
        result.keys = result.keys.join(';')
        result.content = result.content.join(';')
        return result
      }
    }
  },
  data() {
    return {
      keywordMap: ['basic', 'images', 'music', 'news', 'video', 'newsText', 'sappCard'],
      tableFormatData: {
        current: 'userTable',
        userTable: {
          key: 'userTable',
          api: {
            getList: getReplys,
            delList: deleteReply
          },
          tableData: [],
          files: {
            keyword: '',
            keyword_type: '',
            module: 'reply',
            ids: [],
            checkall: '0',
            Recycle: '1'
          },
          change: {
            status: {
              key: 'status',
              api: statusReply
            }
          },
          pagination: {
            page: 1,
            list_rows: 10,
            pagesizes: [10, 20, 50],
            total: 0
          }
        }
      }
    }
  },
  methods: {

  }
}
</script>
